package com.caishi.lkx.exam.api;

import cn.hutool.core.util.StrUtil;
import com.caishi.lkx.common.BaseApi;
import com.caishi.lkx.common.context.RequireContext;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.exam.service.IAdvertisementService;
import com.caishi.lkx.user.model.AdvertisementModel;
import com.zzw.common.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "广告相关接口")
@RestController
@RequestMapping("/banners")
public class BannerApi extends BaseApi {
    @Resource
    private IAdvertisementService advertisementService;


    @Operation(summary = "获取广告列表")
    @Roles
    @RequireContext({com.caishi.lkx.common.context.Role.APP})
    @GetMapping("/list")
    public List<AdvertisementModel> list(AdvertisementModel advertisementModel) {
        advertisementModel.setGroupId(context.getGroupId());
        return advertisementService.selectList(Wrappers.<AdvertisementModel>lambdaQuery().eq(AdvertisementModel::getPositionCode,advertisementModel.getPositionCode())
                        .eq(AdvertisementModel::getGroupId, advertisementModel.getGroupId())
                .eq(StrUtil.isNotBlank(advertisementModel.getExamId()), AdvertisementModel::getExamId, advertisementModel.getExamId())
                .isNull(StrUtil.isBlank(advertisementModel.getExamId()), AdvertisementModel::getExamId)
                .or(StrUtil.isNotBlank(advertisementModel.getExamId()),r->r.isNull(AdvertisementModel::getExamId).eq(AdvertisementModel::getPositionCode,advertisementModel.getPositionCode())).orderByDesc(AdvertisementModel::getExamId).orderByAsc(AdvertisementModel::getSort));
    }
}
